package com.xnew.org.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.xnew.bean.common.OrgDeptInfo;
import com.xnew.bean.common.SysUserInfo;
import com.xnew.org.mapper.OrgDeptInfoMapper;
import com.xnew.org.service.OrgDeptService;
import com.xnew.userOrg.mapper.SysOrgUserMapper;
import com.xnew.utils.DataInput;
import com.xnew.utils.DataOutput;
import com.xnew.utils.IdUtils;
import com.xnew.utils.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author: hexs
 * @Project: JavaLaity
 * @Pcakage: com.xnew.org.service.impl.OrgDeptServiceImpl
 * @Date: 2022年05月31日 16:51
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrgDeptServiceImpl implements OrgDeptService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private OrgDeptInfoMapper orgDeptInfoMapper;

    @Resource
    private SysOrgUserMapper sysOrgUserMapper;

    /**
     * 获取当前登录结构信息
     * @param userInfo
     * @return
     */
    @Override
    public List<Map<String, String>> getCurrentLoginInfo(SysUserInfo userInfo) {
        return sysOrgUserMapper.getCurrentLoginInfo(userInfo.getOrgUserNo());
    }

    /**
     * 组织结构查询列表
     * @param input
     * @return
     */
    @Override
    public List<OrgDeptInfo> queryOrgDeptList(DataInput input, SysUserInfo userInfo) {
        PageHelper.startPage(input.getPageNum(), input.getPageSize());
        Map param = input.getParam();
        param.put("userInfoNo", userInfo.getUserInfoNo());
        return orgDeptInfoMapper.queryOrgDeptList(param);
    }

    /**
     * 新增组织结构
     * @param orgDeptInfo
     * @param request
     * @return
     */
    @Override
    public DataOutput addOrgDept(OrgDeptInfo orgDeptInfo, HttpServletRequest request) {
        DataOutput out = DataOutput.success();
        try {
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            orgDeptInfo.setDeptNo(IdUtils.getIncreaseIdByNanoTime());//id
            orgDeptInfo.setCreorgId(userInfo.getOrgUserNo());//创建人机构编号
            orgDeptInfo.setCreatePsnNo(userInfo.getUserInfoNo());//创建人
            orgDeptInfo.setCreateTime(DateUtil.now());//创建时间
            orgDeptInfo.setPrnDeptNo(userInfo.getBeltoDeptNo());
            String deptLvl = orgDeptInfoMapper.getDeptLvl(userInfo.getBeltoDeptNo());
            orgDeptInfo.setDeptLvl(deptLvl);
            orgDeptInfoMapper.insert(orgDeptInfo);
        }catch (Exception e){
            logger.error(e.getMessage());
            out.setCode(-90001);
            out.setMsg("操作失败!");
        }
        return out;
    }

    /**
     * 修改组织结构
     * @param orgDeptInfo
     * @param request
     * @return
     */
    @Override
    public DataOutput updateOrgDept(OrgDeptInfo orgDeptInfo, HttpServletRequest request) {
        DataOutput out = DataOutput.success();
        try {
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            orgDeptInfo.setUpdatePsnNo(userInfo.getUserInfoNo());//修改人
            orgDeptInfo.setUpdateTime(DateUtil.now());//修改时间
            orgDeptInfoMapper.updateById(orgDeptInfo);
        }catch (Exception e){
            logger.error(e.getMessage());
            out.setCode(-90001);
            out.setMsg("操作失败!");
        }
        return out;
    }

    /**
     * 查询组织结构
     * @param deptNo
     * @return
     */
    @Override
    public OrgDeptInfo queryOrgDeptById(String deptNo) {
        return orgDeptInfoMapper.selectById(deptNo);
    }

    /**
     * 删除组织结构
     * @param deptNo
     */
    @Override
    public void deleteOrgDeptById(String deptNo) {
        orgDeptInfoMapper.deleteById(deptNo);
    }
}
